package com.jic.order.base;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.RestResult;
import com.jic.order.enums.OrderEnum;
import com.jic.order.exception.OrderException;
import com.jic.order.model.OrderModel;
import com.jic.order.model.ParentOrderModel;
import com.jic.order.model.domain.OperateModel;
import com.jic.order.model.domain.OrderDeliverModel;
import com.jic.order.service.ModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
public class BaseService {

	@Autowired
	private ModelService modelService;

	/**
	 * 查询接口通用
	 */
	protected <Q> RestResult<Q> executeQuery(Query<Q> query) {
		RestResult<Q> result = new RestResult();
		try {
			long start = System.currentTimeMillis();

			// 服务调用
			Q q = query.query();

			result.setCode(OrderEnum.SUCCESS.getCode());
			result.setMsg(OrderEnum.SUCCESS.getMessage());
			result.setData(q);

			long end = System.currentTimeMillis();
			log.info("查询服务返回，用时,{},{}", end - start, JSON.toJSONString(result));
		} catch (OrderException fe) {
			log.info("订单异常", fe);
			result.setCode(fe.getOrderEnum().getCode());
			result.setMsg(fe.getMessage());
		} catch (Exception e) {
			log.info("出现未知异常", e);
			result.setCode(OrderEnum.UNKOWN_EXECEPTION.getCode());
			result.setMsg(OrderEnum.UNKOWN_EXECEPTION.getMessage());
		}
		return result;
	}

	/**
	 * 非查询接口订单级别通用
	 */
	protected <T, M> RestResult<T> executeOperate(OperateModel operateModel, BaseService.TemplateService<T, M> service) {
		RestResult<T> result = new RestResult();
		try {
			long start = System.currentTimeMillis();

			//TODO 用redid做防重复提交处理，用请求流水号+操作动作做key，业务处理完毕后清除该数据
			//如果业务正在处理中，返回请勿重复操作
			//如果业务已经处理完毕，返回同样的结果，需要在operator表中加流水号（流水号和操作动作组合唯一）和返回结果

			M model = service.init();
			T t = null;
			//加载模型
			if (model instanceof OrderModel) {
				modelService.fillOrder((OrderModel) model);
				// 处理前状态
				operateModel.setOriginOrderStatus(new Integer(((OrderModel) model).getOrderStatus()));
				// 处理业务
				t = service.process(model);
				// 处理后状态
				operateModel.setCurrentOrderStatus(((OrderModel) model).getOrderStatus());
				operateModel.setResult(t);
				modelService.saveOrder((OrderModel) model, operateModel);
			} else if (model instanceof OrderDeliverModel) {
				modelService.fillDeliver((OrderDeliverModel) model);
				// 处理业务
				t = service.process(model);
				operateModel.setResult(t);
				modelService.saveDeliver((OrderDeliverModel) model, operateModel);
			} else if (model instanceof ParentOrderModel) {
				modelService.fillParentOrder((ParentOrderModel) model);
				// 处理业务
				t = service.process(model);
				operateModel.setResult(t);
				modelService.saveParentOrder((ParentOrderModel) model, operateModel);
			}
			// 返回结果
			result.setCode(OrderEnum.SUCCESS.getCode());
			result.setMsg(OrderEnum.SUCCESS.getMessage());
			result.setData(t);
			long end = System.currentTimeMillis();
			log.info("操作服务返回，耗时{},{}", end - start, JSON.toJSONString(result));
			return result;
		} catch (OrderException fe) {
			log.info("订单异常", fe);
			result.setCode(fe.getOrderEnum().getCode());
			result.setMsg(fe.getMessage());
		} catch (Exception e) {
			log.info("出现未知异常", e);
			result.setCode(OrderEnum.UNKOWN_EXECEPTION.getCode());
			result.setMsg(OrderEnum.UNKOWN_EXECEPTION.getMessage());
		}
		return result;
	}

	protected interface Query<Q> {

		Q query();
	}

	protected interface TemplateService<T, M> {

		M init();

		//处理并返回结果
		T process(M model);

	}

}
